/**
 * 车辆基本信息
*
*/
var express = require('express');
var request = require('request');

var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var auth = require('../../auth/auth');
var util = require('../../util/commonutil');
var multer  = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'public/uploads/');
    },
    filename: function (req, file, cb) {
        var fname=file.originalname;
        var pixs="";
        if(fname){
            pixs=fname.split(".")[1];
        }
        cb(null,  file.fieldname + '-' + Date.now()+"."+pixs);
    }
})

var upload = multer({ storage: storage });
var router = express.Router();
// var querystring = require('querystring');

router.get('/', function(req, res, next) {
   auth.checkAuth(req, res, next);
   res.render('baseCar/index',{serverurl:pro.serveraddr});
   // res.render('baseCar/upload');
});
router.get('/commonSet', function(req, res, next) {
   auth.checkAuth(req, res, next);
   res.render('baseCar/commonSet');
});
router.get('/sendDis', function(req, res, next) {
   auth.checkAuth(req, res, next);
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    //TODO 注释下方一行，打开注释
    res.json({success:true});
    // request({
    //     url: pro.serveraddr+'/baseCar/sendDis',
    //     method: "POST",
    //     qs: arg
    // }, function(error, response, body) {
    //     if (!error && response.statusCode == 200) {
    //         var data=JSON.parse(body);
    //         if(data && data.success){
    //             res.json(data);
    //         }else {
    //             console.log("error= " + data.msg );
    //             res.json(data.msg);
    //         }
    //
    //
    //
    //     }
    // });
});
router.get('/list', function(req, res){
   var pathname = url.parse(req.url).pathname;  //pathname => list

   var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
   request({
       url: pro.serveraddr+'/baseCar/list',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.json(data);
            }else {
                console.log("error= " + data.msg );
                res.json(data.msg);
            }



        }
    });
});
router.get('/edit', function(req, res){
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    request({
        url: pro.serveraddr+'/baseCar/edit',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        console.log(JSON.parse(body));
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.json(data.obj);
            }else {
                console.log("error= " + data.msg );
                res.json(data.msg);
            }
        }
    });
});
router.get('/create', function(req, res){
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    request({
        url: pro.serveraddr+'/baseCar/create',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.json(data.obj);
            }else {
                console.log("error= " + data.msg );
                res.json(data.msg);
            }
        }
    });
});
router.post('/save', function(req, res){
    // req.params，req.query 和 req.body 中的表单数据
    request({
        url: pro.serveraddr+'/baseCar/save',
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
        },
        qs: req.body
    }, function(error, response, body) {
        console.log("保存车辆信息>>"+JSON.stringify(body));
        if (!error && response.statusCode == 200) {
            var info = body;
            res.json(info);
            // res.send();
        }else if (response.statusCode == 500) {
            res.send("服务错误!");
            console.log("error= 服务错误");
            // res.send();
        }else{
            console.log("error= " + error );
        }

    });
});
router.get('/del', function(req, res){
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    request({
        url: pro.serveraddr+'/baseCar/delete',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.json(data.msg);
            }else if (response.statusCode == 500) {
                res.send("服务错误!");
                console.log("error= 服务错误");
                // res.send();
            }else{
                console.log("error= " + error );
            }
        }
    });
});
router.get('/exportExcel', function(req, res){
    var pathname = url.parse(req.url).pathname;  //pathname => list

    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query=req.query;//参数
    request({
        url: pro.serveraddr+'/baseCar/exportExcel',
        method: "POST",
        qs: arg
    }, function(error, response, body) {
        res.json("export successfully!");
    });
});
router.post('/file-upload', upload.single('thumbnail'), function(req, res, next) {
    var file = req.file;

    // var formData = {
    //     // Pass a simple key-value pair
    //     my_field: 'file',
    //     // Pass data via Buffers
    //     my_buffer: new Buffer([1, 2, 3]),
    //     // Pass data via Streams
    //     my_file: file,
    //     // Pass multiple values /w an Array
    //     // attachments: [
    //     //     fs.createReadStream(__dirname + '/attachment1.jpg'),
    //     //     fs.createReadStream(__dirname + '/attachment2.jpg')
    //     // ],
    //     // Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
    //     // Use case: for some types of streams, you'll need to provide "file"-related information manually.
    //     // See the `form-data` README for more information about options: https://github.com/form-data/form-data
    //     // custom_file: {
    //     //     value:  fs.createReadStream('/dev/urandom'),
    //     //     options: {
    //     //         filename: 'topsecret.jpg',
    //     //         contentType: 'image/jpeg'
    //     //     }
    //     // }
    // };
    console.log('文件类型：%s', file.mimetype);
    console.log('原始文件名：%s', file.originalname);
    console.log('文件大小：%s', file.size);
    console.log('文件保存路径：%s', file.path);
    res.json("export successfully!");
    // request({
    //     url: pro.serveraddr+'/upload/imgFile',
    //      formData: formData}, function optionalCallback(err, httpResponse, body) {
    //     if (err) {
    //         return console.error('upload failed:', err);
    //     }
    //     console.log('Upload successful!  Server responded with:', body);
    // });
});
module.exports = router;
